package com.yangoxix.lovegoing.controller;

import com.yangoxix.lovegoing.service.PublisherService;
import com.yangoxix.lovegoing.service.UserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/person")
@RestController
@Slf4j
@Api(tags="活动发布者接口")
public class PublisherController {
    private final PublisherService publisherService;
    private final UserService userService;
    private static final String AUDIT_ACCEPT_COMMAND = "success";
    private static final String AUDIT_DENIAL_COMMAND = "fail";


    public PublisherController(PublisherService publisherService, UserService userService) {
        this.publisherService = publisherService;
        this.userService = userService;
    }

    @PostMapping("applyForPublisher")
    public ResponseEntity<String> tryApply(@RequestParam("name") String username,
                                           @RequestParam("reason") String reason) {
        if (!userService.isUserExisted(username))
            return entityOfUserNotExisted();
        else if (publisherService.isPublisher(username))
            return entityOfIsPublisher();
        else if (publisherService.isApplyExisted(username))
            return entityOfInProgress();
        else {
            publisherService.applyPublisher(username, reason);
            return entityOfApplySubmitted();
        }
    }

    public ResponseEntity<String> entityOfUserNotExisted() {
        return new ResponseEntity<>("用户不存在", HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<String> entityOfIsPublisher() {
        return new ResponseEntity<>("已是发布者", HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<String> entityOfInProgress() {
        return new ResponseEntity<>("请勿重复提交", HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<String> entityOfApplySubmitted() {
        return new ResponseEntity<>("申请已提交", HttpStatus.ACCEPTED);
    }

    @GetMapping("auditPublisher")
    public ResponseEntity<String> tryAudit(@RequestParam("name") String username,
                                           @RequestParam("result") String result) {
        if (!publisherService.isApplyExisted(username)) {
            return entityOfApplyNotExisted();
        } else if (result.equals(AUDIT_ACCEPT_COMMAND)) {
            publisherService.auditAccept(username);
            return entityOfAuditAccepted();
        } else if (result.equals(AUDIT_DENIAL_COMMAND)) {
            publisherService.auditDeny(username);
            return entityOfAuditDenied();
        } else {
            return entityOfUnknownCommand();
        }
    }

    private ResponseEntity<String> entityOfApplyNotExisted() {
        return new ResponseEntity<>("不存在此用户申请活动发布者的请求", HttpStatus.BAD_REQUEST);
    }

    private ResponseEntity<String> entityOfAuditAccepted() {
        return new ResponseEntity<>("审核通过", HttpStatus.OK);
    }

    private ResponseEntity<String> entityOfAuditDenied() {
        return new ResponseEntity<>("审核拒绝", HttpStatus.OK);
    }

    private ResponseEntity<String> entityOfUnknownCommand() {return new ResponseEntity<>("未知指令", HttpStatus.BAD_REQUEST); }
}
